/* Universidad del Valle de Guatemala
 * Mariana Castillo, 12374
 * Julio Maldonado, 12377
 * Seccion 30, Algoritmos y Estructura de Datos
 * Fecha: 03/07/13
 * SINGLY LINKED LIST
 */
package hojat4;


public class SinglyLinkedList<E> extends AbstractList<E>
{

    protected int count; // list size
    protected Node<E> head; // ref. to first element

    public SinglyLinkedList()
    // post: generates an empty list
    {
       head = null;
       count = 0;
    }

    public int size()
    // post: returns number of elements in list
   {
     return count;
   }

   public void addFirst(E value)
   // post: value is added to beginning of list
   {
      // note order that things happen:
      // head is parameter, then assigned
      head = new Node<E>(value, head);
      count++;
   }

   public E removeFirst()
   // pre: list is not empty
   // post: removes and returns value from beginning of list
  {
      Node<E> temp = head;
      head = head.next(); // move head down list
      count--;
      return temp.value();
   }

   public E getFirst()
   // pre: list is not empty
   // post: returns first value in list
   {
       return head.value();
   }

   public void addLast(E value)
   // post: adds value to end of list
   {
       // location for new value
       Node<E> temp = new Node<E>(value,null);
       if (head != null)
      {
          // pointer to possible tail
          Node<E> finger = head;
          while (finger.next() != null)
          {
                 finger = finger.next();
          }

          finger.setNext(temp);
       } else head = temp;

       count++;

    }


    public boolean contains(E value)
    // pre: value is not null
    // post: returns true iff value is found in list
   {
       Node<E> finger = head;

       while (finger != null &&
              !finger.value().equals(value))
      {
           finger = finger.next();
       }
       return finger != null;
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   
    @Override
    public E removeLast() {
        Node<E> finger= head;
        Node<E> previous= null;
        //Assert.pre(head != null, "Lista No esta Vacia");
        while(finger.next() != null){ //Encuentra el final de la lista
            previous=finger;
            finger= finger.next();
        }
        //Si finger es null, o el final de la lista
        if(previous ==null){
            head=null;
        }
        else{
            previous.setNext(null);         
        }
        count--;
        return finger.value();
    }

    //Regrese el elemento head de la lista
    public E get() {
        return head.value();
    }

    @Override
    public int indexOf(E value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    
}